﻿using LJ.Abstractions.Extensions;
using LJ.Abstractions.Pack;
using LJ.DataAccess.Abstractions;
using LJ.DataAccess.EntityFrameworkCore.Internal;
using LJ.DataAccess.EntityFrameworkCore.Pack;
using LJ.DataAccess.EntityFrameworkCore.PostgreSQL.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace LJ.DataAccess.EntityFrameworkCore.PostgreSQL.Pack;


public class DataAccessEntityFrameworkCoreSqlitePackBase : PackBase, IDataAccessEntityFrameworkCorePack
{

    public override void Add(IPackContext packContext)
    {

        var dataProviderManager = new EFDataProviderManager(packContext.DependencyManager, packContext.ReflectionManager);

        packContext.DependencyManager.ServiceCollection.AddSingleton<IDataProviderManager>(dataProviderManager);

        var dataObjectMapManager = new EFDataObjectMapManager(packContext.ReflectionManager);

        packContext.DependencyManager.ServiceCollection.AddSingleton<IDataObjectMapManager>(dataObjectMapManager);

        packContext.DependencyManager.ServiceCollection.AddScoped<ITransactionalProvider, EFPostgreSQLTransactionalProvider>();



        packContext.ConfigManager.Add<DataAccessConfig>("DataAccess");

        packContext.DependencyManager.ServiceCollection.AddDbContextFactory<DataAccessDbContext>((a, b) =>
        {
            var dataAccessConfig = a.GetConfig<DataAccessConfig>();
            b.UseNpgsql(dataAccessConfig.ConnectionString, c =>
            {
                c.MigrationsAssembly(dataAccessConfig.MigrationsAssembly);
            });
        });
    }

    public override void Use(IPackProvider packProvider)
    {
    }
}
